//
//  MeAddressNewVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/24.
//

#import "MeAddressNewVC.h"
#import "MeEditAddressCell.h"
#import "EditAddressFootView.h"
#import <BRPickerView.h>

@interface MeAddressNewVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign) NSInteger type;//0新增 1编辑
@property (nonatomic,strong) EditAddressFootView * footrView;
@property (nonatomic,strong) BRAddressPickerView* addressView;
@property (nonatomic,strong) UITableView * listView;
@property (nonatomic,strong) AddressListModel * infModel;

@end

@implementation MeAddressNewVC

-(instancetype)initWithType:(NSUInteger)type model:( AddressListModel *)model{
    
    if (self = [super init]) {
        self.type = type;
        self.infModel = model;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
}
-(void)setSubViewLayout{
    
    self.title = self.type == 0 ? @"添加新地址" : @"编辑地址";
    self.navgationView.infoLabel.textColor = RDRGB(0x714933);
    [self.view addSubview:self.listView];

}

-(void)hideKeyBord{
    
    [self.view endEditing:YES];
    
}

-(void)updateUserAddress{
    
    if (!(self.infModel.name.length > 0)) {
        FhudShowStr(@"请输入姓名")
        return;
    }
    if (!(self.infModel.phone.length > 0)) {
        FhudShowStr(@"请输入手机号码")
        return;
    }
    if (self.infModel.phone.length != 11) {
        FhudShowStr(@"请输入正确手机号码")
        return;
    }
    if (!(self.infModel.city.length > 0)) {
        FhudShowStr(@"请选择地址信息")
        return;
    }
    if (!(self.infModel.detailAddress.length > 0)) {
        FhudShowStr(@"请输入详细地址")
        return;
    }
    
    if(self.type == 0 ){
        NSDictionary * params = @{@"name":self.infModel.name,@"phone":self.infModel.phone,@"province":self.infModel.province,@"city":self.infModel.city,@"district":self.infModel.district,@"street":self.infModel.district,@"detailAddress":self.infModel.detailAddress,@"isDefault":@(self.infModel.type)};
       
        [SPNetworkingIns POST:PORT(@"auth-center/api/log/addUserAddress") params:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
            FhudShowStr(@"添加成功");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
    }else{
        NSDictionary * params = @{@"name":self.infModel.name,@"phone":self.infModel.phone,@"province":self.infModel.province,@"city":self.infModel.city,@"district":self.infModel.district,@"street":self.infModel.district,@"detailAddress":self.infModel.detailAddress,@"isDefault":@(self.infModel.type),@"id":self.infModel.ID};
        [SPNetworkingIns POST:PORT(@"auth-center/api/log/updateAddressListById") params:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
            FhudShowStr(@"修改成功");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
        
    }
   
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 59 * FMScaleX;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MeEditAddressCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MeEditAddressCell"];
    [cell setInfoWithIndex:indexPath.row model:self.infModel];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == 2){
        [self.addressView show];
    }
}

-(UITableView *)listView{
    
    if (!_listView) {
        _listView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavHeight + 10, WIDTH, HEIGHT - NavHeight - 10)];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.tableFooterView = self.footrView;
        [_listView registerClass:[MeEditAddressCell class] forCellReuseIdentifier:@"MeEditAddressCell"];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBord)];
        tap.cancelsTouchesInView = NO;
        [_listView addGestureRecognizer:tap];
    }
    return _listView;
}

-(EditAddressFootView *)footrView{
    
    if (!_footrView) {
        _footrView = [[EditAddressFootView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100 * FMScaleX)];
        @WeakObj(self)
        _footrView.enterBtnBlock = ^{
            @StrongObj(self)
            [self updateUserAddress];
        };
    }
    return _footrView;
}

-(BRAddressPickerView *)addressView{
    
    if (!_addressView) {
        _addressView =  [[BRAddressPickerView alloc]init];
        _addressView.pickerMode = BRAddressPickerModeArea;
        _addressView.title = @"请选择地区";
        @WeakObj(self)
        _addressView.resultBlock = ^(BRProvinceModel * _Nullable province, BRCityModel * _Nullable city, BRAreaModel * _Nullable area) {
            @StrongObj(self)
            self.infModel.province = province.name;
            self.infModel.city = city.name;
            self.infModel.district = area.name;
            [self.listView reloadData];
        };
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = UIColor.whiteColor;
        customStyle.pickerTextColor = RDRGB(0x9A7742);
        customStyle.separatorColor = RDRGB(0x9A7742);
        _addressView.pickerStyle = customStyle;
    }
    return _addressView;
}
@end
